(function($, doc) {
	$.plusReady(function() {
		mui.init();
		var user = app.getState();
		var uid = user.user_id;
		var postpass = md5(user.password);
		var self = plus.webview.currentWebview();
		var token = self.token;
		var price = self.price;

		//console.log(token);
		var coindata = ethscan.getCoindata(token);
		var ctype = coindata['ctype'];
		var myWallet = ethscan.getWallet(ctype);
		var address = myWallet.address;
		if(!address) {
			$.alert("请创建钱包！");
			plus.webview.currentWebview().close();
		}
		var pricelist = ethscan.getpriceList();
		var coinname = coindata['name'];
		var currentpirce = pricelist[coinname];
		var sysprice = pricelist['GATC'];
		//console.log(currentpirce);
		//console.log(sysprice);
		//console.log(address);
		doc.getElementById('address').value = address;
		doc.getElementById('coinname').innerHTML = coinname;
		doc.getElementById('coinname1').innerHTML = coinname;
		doc.getElementById('gatcprice').innerHTML = '$' + sysprice;
		doc.getElementById('currentname').innerHTML = coinname;
		doc.getElementById('currentprice').innerHTML = '$' + currentpirce;

		function toDecimal(x) {
			var f = parseFloat(x);
			if(isNaN(f)) {
				return;
			}
			f = Math.round(x * 100) / 100;
			return f;
		}
		doc.getElementById('money').addEventListener('input', function() {
			var val = this.value;
			if(!val || val == '' || isNaN(val)) {
				doc.getElementById('showdesc').innerHTML = '';
				return;
			}
			console.log(val);
			var num = val * sysprice / currentpirce;
			console.log(num);
			doc.getElementById('showdesc').innerHTML = toDecimal(num);
			doc.getElementById('count').value = toDecimal(num);
		});
		var getCodeBox = document.getElementById('getCode');
		var c1 = 60;
		getCodeBox && getCodeBox.addEventListener('tap', function() {
			console.log(appurl + 'ChatApi/getsms');
			mui.post(appurl + 'ChatApi/getsms', {
				u: app.getState().user_id,
				smstype: 2
			}, function(res) {
				console.log(JSON.stringify(res));
				if(res.status == 1) {
					mui.toast('发送短信成功');
				} else {
					mui.toast(res.msg);
					getCodeBox.innerHTML = '获取验证码';
					getCodeBox.removeAttribute('disabled');
					c1 = 60;
					return;
				}
				var code1 = setInterval(function() {
					getCodeBox.innerHTML = c1 + '秒后重新获取';
					c1 = c1 - 1;
					if(c1 >= 0) {
						getCodeBox.setAttribute("disabled", "disabled");
					} else {
						getCodeBox.innerHTML = '获取验证码';
						getCodeBox.removeAttribute('disabled');
						c1 = 60;
						clearInterval(code1);
					}
				}, 1000);
			});
		})
		var url = weburl + "/home/ChatApi/userinfo.html?u=" + uid + "&p=" + postpass;
		//console.log(url);
		mui.getJSON(url, function(res) {
			if(res.status == 1) {
				doc.getElementById("hascount").value = res.data.a1_money;
			}
		});
		doc.getElementById('btn').addEventListener('tap', function() {
			var hascount = doc.getElementById("hascount").value * 1;
			var count = doc.getElementById('count').value * 1;
			var money = doc.getElementById('money').value * 1;
			var passwd = doc.getElementById('passwd').value;
			var showdesc = doc.getElementById('showdesc').innerHTML * 1;
			console.log(hascount);
			console.log(money);
			var code = document.getElementById('code').value;
			if(!money || isNaN(money) || money <= 0) {
				$.alert('出售的数量必须为大于0的数字');
				doc.getElementById('money').value = '';
				doc.getElementById('money').focus();
				return;
			}
			if(!count || isNaN(count) || count <= 0) {
				$.alert('购买的数量必须为大于0的数字');
				doc.getElementById('count').value = '';
				doc.getElementById('count').focus();
				return;
			}
			if(hascount < money) {
				$.alert('出售的数量不能大于现有的数量');
				doc.getElementById('money').value = '';
				doc.getElementById('money').focus();
				return;
			}
			if(count < showdesc * 0.9) {
				$.alert('购买的数量不能少于' + showdesc * 0.9);
				doc.getElementById('count').value = '';
				doc.getElementById('count').focus();
				return;
			}
			if(count > showdesc * 1.1) {
				$.alert('购买的数量不能大于' + showdesc * 1.1);
				doc.getElementById('count').value = '';
				doc.getElementById('count').focus();
				return;
			}
			if(!passwd) {
				$.alert('交易密码不能为空');
				return;
			}
			if(!code || code == '') {
					mui.alert('请输入手机验证码！');
					return;
				}
			/**
			 * $data['u']  用户id
			 * $data['p1'] 交易密码
			 * $data['btype']  1买，2卖
			 * $data['ctype']  交易icon
			 * $data['c']   交易数量
			 * $data['money']  交易金额，获取的gatc个数
			 * $data['wallet'] 收币钱包地址（买入交易）
			 */
			var postdata = {
				u: uid,
				p1: passwd,
				btype: 1,
				ctype: token,
				c: count,
				money: money,
				wallet: address,
				code : code,
				smstype : 2
			};
			$.post(weburl + 'Home/ChatApi/createTrade', postdata, function(res) {
				console.log(JSON.stringify(res));
				$.alert(res.msg);
				if(res && res.status > 0) {
					var marketWin = plus.webview.getWebviewById('market.html');
					mui.fire(marketWin, 'refreshList', null);
					plus.webview.currentWebview().close();
				}
			});
		}, false);
		//doc.getElementById('seeprice').innerHTML = '$' + price;
		//var defprice = ethscan.getDefprice();

		//console.log(JSON.stringify(coindata));

	});
}(mui, document));